package com.mce.nebula.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mce.nebula.common.BaseEntity;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * 服务源信息实体，对应Python的ServiceSourceInfo模型
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "service_source")
public class ServiceSourceInfo extends BaseEntity {
    
    @OneToOne
    @JoinColumn(name = "service_id", referencedColumnName = "service_id")
    private TenantService service;
    
    @Column(name = "team_id", length = 32, nullable = false)
    private String teamId;
    
    @Column(name = "user_name", length = 255)
    private String userName;
    
    @Column(name = "password", length = 255)
    private String password;
    
    @Column(name = "group_key", length = 32)
    private String groupKey;
    
    @Column(name = "version", length = 32)
    private String version;
    
    @Column(name = "service_share_uuid", length = 65)
    private String serviceShareUuid;
    
    @Column(name = "extend_info", length = 1024)
    private String extendInfo;
    
    /**
     * 是否从云端安装
     */
    public boolean isInstallFromCloud() {
        if (extendInfo != null) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                Map<String, Object> extendInfoMap = mapper.readValue(extendInfo, Map.class);
                return Boolean.TRUE.equals(extendInfoMap.get("install_from_cloud"));
            } catch (JsonProcessingException e) {
                log.warn("Failed to parse extend_info", e);
                return false;
            }
        }
        return false;
    }
    
    /**
     * 获取市场名称
     */
    public String getMarketName() {
        if (extendInfo != null) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                Map<String, Object> extendInfoMap = mapper.readValue(extendInfo, Map.class);
                return (String) extendInfoMap.get("market_name");
            } catch (JsonProcessingException e) {
                log.warn("Failed to parse extend_info", e);
                return null;
            }
        }
        return null;
    }
    
    /**
     * 获取模板更新时间
     */
    public LocalDateTime getTemplateUpdateTime() {
        if (extendInfo != null) {
            try {
                ObjectMapper mapper = new ObjectMapper();
                Map<String, Object> extendInfoMap = mapper.readValue(extendInfo, Map.class);
                String updateTime = (String) extendInfoMap.get("update_time");
                if (updateTime != null) {
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    return LocalDateTime.parse(updateTime, formatter);
                }
            } catch (Exception e) {
                log.warn("Failed to parse template update time", e);
            }
        }
        return null;
    }
}